//
//  NSObject+YTAdd.m
//  tesDemo02
//
//  Created by 拓客营 on 2022/3/21.
//

#import "NSObject+YTAdd.h"
#import <UIKit/UIKit.h>

dispatch_queue_t get_global_queue(){
    return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
}

void run_main_queue(void (^block)(void)) {
    dispatch_async(dispatch_get_main_queue(), block);
}

void run_global_queue(void (^block)(void)) {
    dispatch_async(get_global_queue(), block);
}

void run_main_after(float sec, void(^block)(void)) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec * NSEC_PER_SEC)), dispatch_get_main_queue(), block);
}

@implementation NSObject (YTAdd)

static BOOL _isPhoneX = NO;
static CGFloat _screenScale = 0.f;

+ (BOOL)isIphoneX {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 利用safeAreaInsets.bottom > 0.0来判断是否是iPhone X以上设备。
        if (@available(iOS 11.0, *)) {
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            if (window.safeAreaInsets.bottom > 0.0) {
                _isPhoneX = YES;
            }
        }
    });
    
    return _isPhoneX;
}

+ (CGFloat)statusBarHeight {
    return self.isIphoneX ? 44.f : 20.f;
}

+ (CGFloat)currentStatusBarHeight {
    if (@available(iOS 13, *)) {
        return UIApplication.sharedApplication.windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;
    }
    
    return [[UIApplication sharedApplication] statusBarFrame].size.height;
}

+ (CGFloat)naviBarHeight {
    return self.isIphoneX ? 88.f : 64.f;
}

+ (CGFloat)homeIndecatorHeight {
    return self.isIphoneX ? 34.f : 0.f;
}

+ (CGFloat)tabBarHeight {
    return self.isIphoneX ? 83.f : 49.f;
}

+ (CGFloat)screenScale {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_screenScale == 0.f) {
            _screenScale = [UIScreen mainScreen].bounds.size.width / 375.f;
        }
    });
    return _screenScale;
}

@end
